//
//  ViewController.m
//  UI01_UIView
//
//  Created by Rain on 16/6/3.
//  Copyright © 2016年 Rain. All rights reserved.
//

#import "ViewController.h"
#import "CustomView.h"

@interface ViewController ()

@property (nonatomic, retain) CustomView *rainView;

@end

@implementation ViewController

// 对 对象类型的属性进行内存管理
- (void)dealloc
{
    [_rainView release];
    
    [super dealloc];
}


#pragma mark - UIViewController 生命周期

// 如果要重写UIViewController的初始化方法的话, 一般都重写这个方法.
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        NSLog(@"%d, %s", __LINE__, __FUNCTION__);
        
    }
    
    return self;
    
}

- (instancetype)init {
    
    self = [super init];
    if (self) {
        NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    }
    
    return self;
    
}

// 加载视图
- (void)loadView {
    
    [super loadView];
    
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
}

// 视图加载完成, 加载子视图
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    
    self.view.backgroundColor = [UIColor greenColor];
    
//    [self creadSubViews];
    
    [self addCustomViews];
    
    [self creatButtons];
    
}
// 视图将要显示
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
    
}

// 视图显示完成
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"%d, %s", __LINE__, __FUNCTION__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 知识点2 UIView

// 创建自定义方法, 写加载子视图的代码.
- (void)creadSubViews {
    
    // 创建视图
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 200, 300)];
    
    // 视图背景颜色默认为clearColor.
    blueView.backgroundColor = [UIColor blueColor];
    
    
    // 将子视图添加到父视图上. self.view是blueView的父视图.
    [self.view addSubview:blueView];
    
    // 内存管理
    [blueView release];
    
    // 练习: 创建一个视图, 距离屏幕左边50, 距离屏幕上边400, 宽 50, 高 30, 背景颜色为黄色. 添加到self.view视图上.
    
    UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(50, 400, 50, 30)];
    
    yellowView.backgroundColor = [UIColor yellowColor];
    
    [self.view addSubview:yellowView];
    [yellowView release];
    
    // 将blackView放到blueView上.
    UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 30, 50)];
    blackView.backgroundColor = [UIColor blackColor];
    
    [blueView addSubview:blackView];
    [blackView release];
    
    
    /**
     * frame是以父视图为参照的坐标系统.
     */
    
    
    // UIView常用API
    
    // 隐藏视图, 默认值为NO, 为YES时将视图隐藏.
//    blueView.hidden = YES;
    
    // 改变视图的透明度, CGFloat类型, 范围从0.0 到 1.0, 默认值为1.0. 1.0是完全不透明状态.
    blueView.alpha = 0.5;
    
    // 获取视图的子视图
    NSLog(@"blueView subViews: %@", blueView.subviews);
    
    // 获取视图的父视图(父视图只有唯一的一个).
    NSLog(@"blueView superView: %@", blueView.superview);
    
    // 视图中心点坐标(对角线交叉的地方.)
    blueView.center = CGPointMake(100, 200);
    
    NSLog(@"x: %lf, y: %lf, w: %lf, h: %lf", blueView.frame.origin.x, blueView.frame.origin.y, blueView.frame.size.width, blueView.frame.size.height);
    
    // 视图的标记. tag
    blueView.tag = 100;
    
    // 通过tag找到指定视图.
    UIView *findView = [self.view viewWithTag:100];
    
    NSLog(@"blueView: %@", blueView);
    NSLog(@"findView: %@", findView);
    
   
    // 输出一下self.view的子视图.
    NSArray *svSubViewsArray = self.view.subviews;
    NSLog(@"self.view 的子视图: %@", svSubViewsArray);
    
    // 创建一个视图, 坐标为(50, 100), 宽高为(200, 300), 颜色为橙色, 添加到self.view上.
    UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 300)];
    orangeView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:orangeView];
    [orangeView release];
    
    
    // 创建一个视图, 坐标为(100, 150), 宽高为(200, 300), 颜色为紫色, 添加到self.View上.
    
    UIView *purleView = [[UIView alloc] initWithFrame:CGRectMake(100, 150, 200, 300)];
    purleView.backgroundColor = [UIColor purpleColor];
    
    [self.view addSubview:purleView];
    [purleView release];
    
    // 把blueView 放到所有子视图的前面.
    [self.view bringSubviewToFront:blueView];
    
    
    // 把purleView 放到所有子视图的后面.
    [self.view sendSubviewToBack:purleView];
    
    // 创建视图深灰色视图, frame (), 添加到下标为2的位置上.
    UIView *darkGrayView = [[UIView alloc] initWithFrame:CGRectMake(150, 250, 200, 300)];
    darkGrayView.backgroundColor = [UIColor darkGrayColor];
    [self.view insertSubview:darkGrayView atIndex:2];
    [darkGrayView release];
    
    // 将视图插入到orangeView的前面.
    UIView *cyanView = [[UIView alloc] initWithFrame:CGRectMake(200, 300, 100, 200)];
    cyanView.backgroundColor = [UIColor cyanColor];
    [self.view insertSubview:cyanView aboveSubview:orangeView];
    [cyanView release];
    
    // 将brownView插入到blueView的后面.
    UIView *brownView = [[UIView alloc] initWithFrame:CGRectMake(30, 20, 50, 50)];
    brownView.backgroundColor = [UIColor brownColor];
    [self.view insertSubview:brownView belowSubview:blueView];
    [brownView release];
    
    
    // 交换指定下标位置上的视图
    [self.view exchangeSubviewAtIndex:4 withSubviewAtIndex:0];
    
    // 将子视图从父视图上移除.
    [brownView removeFromSuperview];
    
    
}

// 添加自定义的视图.
- (void)addCustomViews {
    
    CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(50, 100, 100, 200)];
    
    customView.tag = 100;
    
    [self.view addSubview:customView];
    [customView release];
    
    CustomView *secondView = [[CustomView alloc] initWithFrame:CGRectMake(100, 200, 50, 50) withColor:[UIColor greenColor]];
    [self.view addSubview:secondView];
    [secondView release];
    
    // 创建属性
    self.rainView = [[CustomView alloc] initWithFrame:CGRectMake(200, 500, 100, 200) withColor:[UIColor blackColor]];
    [self.view addSubview:_rainView];
    [_rainView release];
    
}


#pragma mark - UIButton
- (void)creatButtons {
    
    // 创建button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    // 设置背景颜色
    button.backgroundColor = [UIColor orangeColor];
    // 确定在父视图的位置
    button.frame = CGRectMake(100, 400, 50, 40);
    // 添加到父视图上.
    [self.view addSubview:button];
    
    // 不需要对button进行release.
    
    // 添加点击事件
    [button addTarget:self action:@selector(changeColorAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}

// parameterButton 就是 button
- (void)changeColorAction:(UIButton *)parameterButton {
    
   // 改变self.view的背景颜色.
    self.view.backgroundColor= [UIColor grayColor];
    
    // 通过tag值找到指定的视图.
    CustomView *findView = (CustomView *)[self.view viewWithTag:100];
    findView.backgroundColor = [UIColor orangeColor];
    
    // 通过属性改变指定视图的背景颜色.
    self.rainView.backgroundColor = [UIColor whiteColor];
    
    
    
    
}






@end
